function BuildStates(oStateStatusObjs)
{
	var sHtml = "<span class='selectBox' value='" + oStateStatusObjs[0].Value + "' name='StateValue' changeHandler='UpdateStatesStatuses(this.returnValue);'><table cellspacing='0' cellpadding='2' style='display: none;'>";
	
	var i;
	var iLen = oStateStatusObjs.length;
	
	for (i = 0; i < iLen; i++)
	{
		if (i == 0)
		{
			_iState = oStateStatusObjs[i].Value;
		}
		
		sHtml += "<tr><td val='" + oStateStatusObjs[i].Value + "'>" + oStateStatusObjs[i].Labels[0].Description + "</td></tr>";
	}
	
	sHtml +="</table></span>";
	
	StateValues.innerHTML = sHtml;
}


function BuildStatuses(oStateStatusObjs)
{
	var sHtml = "<table cellpadding='2' cellspacing='0' width='100%'><tr style='display: none;'><td class='hand'></td></tr>";

	var i;
	var iLen = oStateStatusObjs.length;

	for (i = 0; i < iLen; i++)
	{	
		if (oStateStatusObjs[i].StateOrDefaultStatus == _iState)
		{
			sHtml += "<tr onclick='SelectRow();' ondblclick='Edit();' value='" + oStateStatusObjs[i].Value + "' state='" + _iState + "'><td nowrap class='hand'>" + oStateStatusObjs[i].Labels[0].Description + "</td></tr>";
		}
	}

	sHtml += "</table>";

	Options.innerHTML = sHtml;
}


function BuildDefaultStatusValues(oStateStatusObjs)
{
	var sHtml = "<span class='selectBox' value='" +  GetStatesDefaultStatus() + "' name='DefaultValue' changeHandler='UpdateStatesStatuses(_iState);'><table cellspacing='0' cellpadding='2' style='display: none;'>";

	var i;
	var iLen = oStateStatusObjs.length;

	for (i = 0; i < iLen; i++)
	{
		if (oStateStatusObjs[i].StateOrDefaultStatus == _iState)
		{
			sHtml += "<tr><td val='" + oStateStatusObjs[i].Value + "'>" + oStateStatusObjs[i].Labels[0].Description + "</td></tr>";
		}
	}

	sHtml +="</table></span>";

	DefaultValues.innerHTML = sHtml;
}


function GetStatesDefaultStatus()
{
	var iLen = _oStates.length;
	
	for (i = 0; i < iLen; i++)
	{
		if (_oStates[i].Value == _iState)
		{
			return _oStates[i].StateOrDefaultStatus;
		}
	}
}


function UpdateStatesStatuses(iState)
{
	// rip through the states and set the current state's defaultstatus
	//
	var iLen = _oStates.length;
	
	for (i = 0; i < iLen; i++)
	{
		if (_oStates[i].Value == _iState)
		{
			_oStates[i].StateOrDefaultStatus = crmDialog.DefaultValue.returnValue;
			
			break;
		}
	}
	
	// rip through the statuses and remove the ones for the current state
	//
	i = 0;
	
	while (i < _oStatuses.length)
	{
		if (_oStatuses[i].StateOrDefaultStatus == _iState)
		{
			_oStatuses.splice(i, 1);
		}
		else
		{
			i++;
		}
	}
	
	// loop through the displayed statuses and push them on the end of the statuses array
	//
	var oStatuses = GetStateStatusObjs();
	
	iLen = oStatuses.length;
	
	for (i = 0; i < iLen; i++)
	{
		_oStatuses.push(oStatuses[i]);
	}
	
	if(_iState != iState)
	{
		// redraw it all
		//
		_iState = iState;
		
		BuildStatuses(_oStatuses);
		BuildDefaultStatusValues(_oStatuses);
	}
	
	_oOption = null;
}


function GetStateStatusObjs()
{
	var oRows = Options.firstChild.rows;
	
	var i;
	var iLen = oRows.length;
	
	var oStateStatusObjs = new Array();

	for (i = 1; i < iLen; i++)
	{
		oStateStatusObjs[i - 1] = new StateStatusObj(parseInt(oRows[i].value, 10), parseInt(oRows[i].state, 10), new Array(new LocalizedObj(HtmlEncode(oRows[i].cells[0].innerText), ORG_LANGUAGE_CODE )));
	}
	
	return oStateStatusObjs;
}